package com.ckv.qcm.server.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ckv.qcm.controller.ResultController;
import com.ckv.qcm.model.Result;
import com.ckv.qcm.model.Trainee;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

/**
 * Servlet implementation class ResultServlet
 */
public class ResultServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ResultServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("ALLLO?");
		RequestDispatcher rd = null;
		String requestDispatcher = "";
		String ret = new String();
		try
		{
			HttpSession session = request.getSession();
			
			Trainee user = (Trainee)session.getAttribute("user");
			
			if(user != null)
			{
				String jsonResults = request.getParameter("results");
				System.out.println(jsonResults);
				Gson gson = new Gson();
				ArrayList<Result> results = new ArrayList<Result>();
				
				JsonParser parser = new JsonParser();
				
				JsonArray array = parser.parse(jsonResults).getAsJsonArray();
				
				for (JsonElement jsonElement : array)
				{
					Result res = gson.fromJson(jsonElement, Result.class);
					res.setIdTrainee(user.getId());
					res.save();
					results.add(res);
				}
				
				ResultController controler = ResultController.getInstance();
				
				HashMap<Long,Boolean> res = controler.Check(results);
				
				for (Long id : res.keySet())
				{
					System.out.println("IdQuestion : " + id + " / correct? : " + res.get(id));
				}
				
				
				ret = gson.toJson(res);
			}
			else
			{
				requestDispatcher = "html/server_error.html";
				rd = request.getRequestDispatcher(requestDispatcher);
				rd.forward(request, response);
			}
			
			response.setContentType("text/html; charset=UTF-8");
			System.out.println(ret);
			PrintWriter out = response.getWriter();
			out.print(ret);
			out.flush();
			out.close();
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
			requestDispatcher = "html/server_error.html";
			rd = request.getRequestDispatcher(requestDispatcher);
			rd.forward(request, response);
		}
	}
}
